<?php
/**
 *
 * 版权所有：顺势网站管理系统<www.51shunshi.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;

class DocumentController extends ComController
{

    public function add()
    {

        $this->display('form');
    }

    public function index($p = 1)
    {
        $p = intval($p) > 0 ? $p : 1;
        $document = M('document');
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $where = '1 = 1 ';
        //默认按照时间降序
        $orderby = "time desc";
        $count = $document->where($where)->count();
        $list = $document->where($where)->order($orderby)->limit($offset . ',' . $pagesize)->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function del()
    {

        $aids = isset($_REQUEST['aids']) ? $_REQUEST['aids'] : false;
        if ($aids) {
            if (is_array($aids)) {
                $aids = implode(',', $aids);
                $map['id'] = array('in', $aids);
            } else {
                $map = 'id=' . $aids;
            }
            if (M('document')->where($map)->delete()) {
                addlog('删除文档，AID：' . $aids);
                $this->success('恭喜，文档删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }

    public function update(){
        if (!empty($_FILES)) {
        //import("@.ORG.UploadFile");
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 2048000;
        $upload->exts = array('txt','doc','docx','pdf');
        $upload->rootPath  =      './Public/'; // 设置附件上传根目录
        $upload->savePath = "document/"; 
       
        $info=$upload->upload();
        // var_dump($info);exit;
        if(!$info){
            $this->error($upload->getError());
        }
        $data['path'] = $info['myfile']['savepath'].$info['myfile']['savename']; 
        $data['title']=$info['myfile']['name'];
        $data['type']=$info['myfile']['ext'];
        $data['size']=$info['myfile']['size'];
        $data['language']=I('post.language', '', 'strip_tags');
        $data['area']=I('post.area', '', 'strip_tags');
        $data['time'] = date("Y-m-d H:i:s");
        
        $result = M("document")->add($data);
        if($result){
            addlog('上传文档，AID：' . $id);
            $this->success('恭喜！文档上传成功！');
        }else{
            addlog('上传文档，AID：' . $id);
            $this->success('文档上传失败！');
        }
        
    }
  }

}